package com.peoit.honeycomb.dto;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.peoit.honeycomb.domain.Banner;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 图片轮播
 * Created by zhouqiu on 2015/10/23.
 */
public class BannerPageDto extends CommonPageQueryDto {
    /**
     * 图片名
     */
    private String name;

    /**
     * 图片标题
     */
    private String title;
    /**
     * 路径
     */
    private String path;

    /**
     * 图片描述
     */
    private String description;

    /**
     * 排序
     */
    private int sortOrder;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getSortOrder() {
        return sortOrder;
    }

    public void setSortOrder(int sortOrder) {
        this.sortOrder = sortOrder;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public static BannerPageDto toDto(Banner banner){
        BannerPageDto dto = new BannerPageDto();
        dto.setId(banner.getId());
        dto.setName(banner.getName());
        dto.setTitle(banner.getTitle());
        dto.setDescription(banner.getDescription());
        dto.setPath(banner.getPath());
        dto.setSortOrder(banner.getSortOrder());
        return dto;
    }

    public static Banner toEntity(BannerPageDto dto){
        Banner banner = new Banner();

        if(StringUtils.isNotBlank(dto.getId()))banner.setId(dto.getId());
        if(StringUtils.isNotBlank(dto.getName()))banner.setName(dto.getName());
        if(StringUtils.isNotBlank(dto.getTitle()))banner.setTitle(dto.getTitle());
        if(StringUtils.isNotBlank(dto.getDescription()))banner.setDescription(dto.getDescription());
        if(StringUtils.isNotBlank(dto.getPath()))banner.setPath(dto.getPath());
        banner.setSortOrder(dto.getSortOrder());
        return banner;
    }

    public List<BannerPageDto> toPageList(List<Banner> banners){
        Collection collection = Collections2.transform(banners, new Function<Banner, BannerPageDto>() {
            @Override
            public BannerPageDto apply(Banner input) {
                return toDto(input);
            }
        });

        ArrayList<BannerPageDto> bannerDtos = Lists.newArrayList(collection);
        return  bannerDtos;
    }


}
